package com.api.workflow.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject;
import weaver.general.BaseBean;

import java.util.HashMap;

public class CrmUtil {

    // 获取token
    public static String getToken() throws JSONException{
        JSONObject params = new JSONObject();
        BaseBean baseBean = new BaseBean();
        try{
            String crm_appId = baseBean.getPropValue("YNSap", "crm_appId");
            String crm_permanentCode = baseBean.getPropValue("YNSap", "crm_permanentCode");
            String crm_appSecret = baseBean.getPropValue("YNSap", "crm_appSecret");
            String crm_url_token_phone = baseBean.getPropValue("YNSap", "crm_url_token_phone");
            String crm_url_getUser = baseBean.getPropValue("YNSap", "crm_url_getUser");

            String tokenUrl = baseBean.getPropValue("YNSap", "crm_url_token");
            JSONObject param = new JSONObject();
            param.put("appId", crm_appId);
            param.put("permanentCode", crm_permanentCode);
            param.put("appSecret", crm_appSecret);
            String message = param.toString();
            baseBean.writeLog("CrmUtil ------------------------------------->getToken tokenUrl:"+tokenUrl + "  message:"+ message);
            String result = HttpUtil.post(tokenUrl, message);
            baseBean.writeLog("CrmUtil ------------------------------------->getToken tokenUrl result:"+ result);
            JSONObject jsonObject = new JSONObject(result);
            String errorCode = jsonObject.getString("errorCode");
            if ("0".equals(errorCode)){
                // token获取成功 获取 currentOpenUserId
                params.put("corpAccessToken", jsonObject.getString("corpAccessToken"));
                params.put("corpId", jsonObject.getString("corpId"));

                JSONObject paramUser = new JSONObject();
                paramUser.put("corpAccessToken", jsonObject.getString("corpAccessToken"));
                paramUser.put("corpId", jsonObject.getString("corpId"));
                paramUser.put("mobile", crm_url_token_phone);
                baseBean.writeLog("CrmUtil ------------------------------------->getToken crm_url_getUser:"+crm_url_getUser + "  message:"+ paramUser);
                String resultUser = HttpUtil.post(crm_url_getUser, paramUser.toString());
                baseBean.writeLog("CrmUtil ------------------------------------->getToken crm_url_getUser result:"+ resultUser);
                JSONObject jsonObjectUser = new JSONObject(resultUser);
                String errorCodeUser = jsonObjectUser.getString("errorCode");
                if ("0".equals(errorCodeUser)){
                    // 手机获取员工信息成功
                    JSONArray empList = jsonObjectUser.getJSONArray("empList");
                    if (empList.length() > 0){
                        JSONObject emp = empList.getJSONObject(0);
                        String openUserId = emp.getString("openUserId");
                        params.put("currentOpenUserId", openUserId);
                    }
                }
            }
        }catch (Exception e){
            baseBean.writeLog("CrmUtil ------------------------------------->getToken e:"+e);
        }
        return params.toString();
    }

    // 修改销售订单
    public static boolean updateSaleOrder(String message,HashMap<String,String> map) throws JSONException{
        BaseBean baseBean = new BaseBean();
        boolean result = false;
        try{
            String dataObjectApiName = baseBean.getPropValue("YNSap", "xs_dataObjectApiName");
            String crm_url_xs = baseBean.getPropValue("YNSap", "crm_url_xs");

            JSONObject params = new JSONObject(message);
            params.put("triggerWorkFlow",true);
            params.put("triggerApprovalFlow",false);

            JSONObject data = new JSONObject();
            JSONObject object_data = new JSONObject(map);
            object_data.put("dataObjectApiName",dataObjectApiName);
            data.put("object_data",object_data);
            data.put("skipDataStatusValidate",true);
            params.put("data",data);

            String post = HttpUtil.post(crm_url_xs, params.toString());
            JSONObject jsonObject = new JSONObject(post);
            String errorCode = jsonObject.getString("errorCode");
            String errormsg = "CrmUtil ------------------------------------->updateSaleOrder crm_url_xs:"+crm_url_xs + "  message:"+ params.toString() + "  post:"+ post;
            baseBean.writeLog(errormsg);
            map.put("errorMsg",errormsg);
            if ("0".equals(errorCode)){
                result = true;
            }
        }catch (Exception e){
            baseBean.writeLog("CrmUtil ------------------------------------->updateSaleOrder e:"+e);
        }
        return result;
    }

    // 修改客诉单
    public static boolean updateKsOrder(String message,HashMap<String,String> map) throws JSONException{
        BaseBean baseBean = new BaseBean();
        boolean result = false;
        try{
            String dataObjectApiName = baseBean.getPropValue("YNSap", "ks_dataObjectApiName");
            String crm_url_xs = baseBean.getPropValue("YNSap", "crm_url_ks");

            JSONObject params = new JSONObject(message);
            params.put("triggerWorkFlow",true);
            params.put("triggerApprovalFlow",false);

            JSONObject data = new JSONObject();
            JSONObject object_data = new JSONObject(map);
            object_data.put("dataObjectApiName",dataObjectApiName);
            data.put("object_data",object_data);
            data.put("skipDataStatusValidate",true);

            params.put("data",data);

            String post = HttpUtil.post(crm_url_xs, params.toString());
            JSONObject jsonObject = new JSONObject(post);
            String errorCode = jsonObject.getString("errorCode");
            String errormsg = "CrmUtil ------------------------------------->updateKsOrder crm_url_xs:"+crm_url_xs + "  message:"+ params.toString() + "  post:"+ post;
            baseBean.writeLog(errormsg);
            map.put("errorMsg",errormsg);
            if ("0".equals(errorCode)){
                result = true;
            }
        }catch (Exception e){
            baseBean.writeLog("CrmUtil ------------------------------------->updateKsOrder e:"+e);
        }
        return result;
    }

    // 修改退货申请单
    public static boolean updateThOrder(String message,HashMap<String,String> map) throws JSONException{
        BaseBean baseBean = new BaseBean();
        boolean result = false;
        try{
            String dataObjectApiName = baseBean.getPropValue("YNSap", "th_dataObjectApiName");
            String crm_url_xs = baseBean.getPropValue("YNSap", "crm_url_th");

            JSONObject params = new JSONObject(message);
            params.put("triggerWorkFlow",true);
            params.put("triggerApprovalFlow",false);

            JSONObject data = new JSONObject();
            JSONObject object_data = new JSONObject(map);
            object_data.put("dataObjectApiName",dataObjectApiName);
            data.put("object_data",object_data);
            data.put("skipDataStatusValidate",true);
            params.put("data",data);

            String post = HttpUtil.post(crm_url_xs, params.toString());
            JSONObject jsonObject = new JSONObject(post);
            String errorCode = jsonObject.getString("errorCode");
            String errormsg ="CrmUtil ------------------------------------->updateThOrder crm_url_xs:"+crm_url_xs + "  message:"+ params.toString() + "  post:"+ post;
            baseBean.writeLog(errormsg);
            map.put("errorMsg",errormsg);
            if ("0".equals(errorCode)){
                result = true;
            }
        }catch (Exception e){
            baseBean.writeLog("CrmUtil ------------------------------------->updateThOrder e:"+e);
        }
        return result;
    }
}
